package com.ilink.groundservice.mapper.innovationPlaza;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ilink.groundservice.entity.PO.innovationPlaza.ProjectMetadata;
import com.ilink.groundservice.entity.VO.innovationPlaza.ProjectResponse;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface ProjectMetadataMapper extends BaseMapper<ProjectMetadata> {

    @Select("SELECT " +
            "ip.project_id, " +
            "pm.name, " +
            "GROUP_CONCAT(it.name) AS tags, " +
            "pm.description, " +
            "pm.like_num, " +
            "pm.share_num, " +
            "pm.look_num, " +
            "pm.project_image, " +
            "pm.createdTime " +
            "FROM innovationPlaza ip " +
            "JOIN project_metadata pm ON ip.project_id = pm.project_id " +
            "LEFT JOIN innovation_connect_tags ict ON ip.project_id = ict.project_id " +
            "LEFT JOIN innovation_tags it ON ict.tag_id = it.tag_id " +
            "GROUP BY ip.project_id, pm.name, pm.description, pm.like_num, pm.share_num, pm.look_num, pm.project_image, pm.createdTime")
    List<ProjectResponse> getAllProjectDetails();

    //仅获取大创项目的meta的结果
    @Select("SELECT " +
            "pm.project_id, " +
            "pm.name, " +
            "pm.description, " +
            "pm.like_num, " +
            "pm.share_num, " +
            "pm.look_num, " +
            "pm.project_image, " +
            "pm.createdTime " +
            "FROM project_metadata pm")
    List<ProjectResponse> getAllProjectMeta();

    @Select("SELECT " +
            "pm.project_id, " +
            "pm.name, " +
            "pm.description, " +
            "pm.like_num, " +
            "pm.share_num, " +
            "pm.look_num, " +
            "pm.createdTime, " +
            "pm.project_image " +
            "FROM project_metadata pm")
    IPage<ProjectResponse> getAllProjectMetaPage(Page<?> page);

    //获取项目的名字
    @Select("SELECT name FROM project_metadata WHERE project_id = #{projectId}")
    String getProjectNameById(Long projectId);

    @Select("SELECT project_style FROM project_metadata WHERE project_id = #{projectId}")
    Integer getProjectStyleById(Long projectId);

    @Select("SELECT advertise FROM project_metadata WHERE project_id = #{projectId}")
    String getSloganByProjectId(Long projectId);

    @Select("SELECT company FROM project_metadata WHERE project_id = #{projectId}")
    String getCompanyByProjectId(Long projectId);

    @Select("SELECT introductors FROM project_metadata WHERE project_id = #{projectId}")
    String getIntroductorsByProjectId(Long projectId);

    @Select("SELECT exhibition_code FROM project_metadata WHERE project_id = #{projectId}")
    String getExhibitionCodeByProjectId(Long projectId);

    //项目详细介绍
    @Select("SELECT introduction, advertise, innovation FROM project_metadata WHERE project_id = #{projectId}")
    ProjectMetadata getProjectItroduce(Long projectId);

    @Update("UPDATE project_metadata SET like_num = like_num + 1 WHERE project_id = #{projectId}")
    void incrementLikeNum(Long projectId);

    @Update("UPDATE project_metadata SET like_num = like_num - 1 WHERE project_id = #{projectId}")
    void decrementLikeNum(Long projectId);

    @Update("UPDATE project_metadata SET look_num = look_num + 1 WHERE project_id = #{projectId}")
    int incrementClickNum(Long projectId);

    @Update("UPDATE project_metadata SET share_num = share_num + 1 WHERE project_id = #{projectId}")
    int incrementShareNum(Long projectId);

    @Select("SELECT * FROM project_metadata WHERE project_id = #{projectId}")
    ProjectMetadata getdetialProjectItroduce(Long projectId);

}
